package schedule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import contract.ContractDTO;
import Product.ProductFactory;
import Product.ProductVo;

public class ScheduleMyba {
	java.util.Calendar cal = java.util.Calendar.getInstance();

	int year = cal.get(cal.YEAR);
	int month = cal.get(cal.MONTH) + 1;

	String temp = Integer.toString(year) + "/" + Integer.toString(month)
			+ "/15";
	private SqlSession session;
	ScheduleVo sv;
	Sv s;
	private String SA_NO = "";
	private String SA_NM = "";

	public String getSA_NM() {
		return SA_NM;
	}

	public void setSA_NM(String sA_NM) {
		SA_NM = sA_NM;
	}

	public SqlSession getSession() {
		return session;
	}

	public void setSession(SqlSession session) {
		this.session = session;
	}

	public String getSA_NO() {
		return SA_NO;
	}

	public void setSA_NO(String sA_NO) {
		SA_NO = sA_NO;
	}

	public ScheduleMyba() {
		try {
			session = ProductFactory.getFactory().openSession();
			// MemberFactory에서 가져다가 사용해도 되는데 굳이 만들필요가읍음

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public boolean insert() {
		boolean flag = false;
		try {
			s = new Sv();
			System.out.println("사원이름:" + sv.getSA_NM());
			s.setTemp(temp);
			System.out.println(temp);
			for (int i = 0; i < sv.getCS_MN().length; i++) {
				temp = s.getTemp();
				temp = (String) session.selectOne("schedule.loan_start", temp);
				s.setTemp(temp);
				System.out.println(temp);
				s.setCS_MN(sv.getCS_MN()[i]);
				s.setCS_OM(sv.getCS_OM()[i]);
				s.setCS_REM(sv.getCS_REM()[i]);
				s.setCS_RM(sv.getCS_RM()[i]);
				s.setCS_SM(sv.getCS_SM()[i]);
				s.setCS_SOM(sv.getCS_SOM()[i]);
				s.setCS_SRM(sv.getCS_SRM()[i]);
				s.setPD_PBACK(sv.getPD_PBACK()[i]);
				s.setPD_PRD(sv.getPD_PRD()[i]);
				s.setPD_RATE(sv.getPD_RATE()[i]);
				s.setCT_NO(sv.getCT_NO()[i]);
				s.setCS_LOAN_START(temp);
				s.setCS_LOAN_END(sv.getCS_LOAN_END()[i]);
				s.setSA_NO(sv.getSA_NO()[i]);
				s.setCS_END(sv.getPD_PRD()[i].substring(0, 2));
				s.setCS_ST(sv.getCS_LOAN_START()[i]);
				s.setSA_NM(sv.getSA_NM());
				/*
				 * System.out.println("회차 : " + s.getCS_ST());
				 * System.out.println("상환방법 : " + s.getPD_PBACK());
				 * System.out.println("대출기간 : " + s.getPD_PRD());
				 * System.out.println("금리 : " + s.getPD_RATE());
				 * System.out.println("계약번호 : " + s.getCT_NO());
				 * System.out.println("END : " + s.getCS_LOAN_END());
				 * System.out.println("START : " + s.getCS_LOAN_START());
				 * System.out.println("MN : " + s.getCS_MN());
				 * System.out.println("OM : " + s.getCS_OM());
				 * System.out.println("REM : " + s.getCS_REM());
				 * System.out.println("RM : " + s.getCS_RM());
				 * System.out.println("SM : " + s.getCS_SM());
				 * System.out.println("SOM : " + s.getCS_SOM());
				 * System.out.println("SRM : " + s.getCS_SRM());
				 */

				int rs = session.insert("schedule.insert", s);
				if (rs > 0) {
					flag = true;
					session.commit();
				} else {
					flag = false;
					session.rollback();
				}

			}

			session.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	public ArrayList<Sv> list() {
		ArrayList<Sv> data = null;

		try {
			System.out.println(1);
			data = (ArrayList<Sv>) session.selectList("schedule.sclist", SA_NO);
			System.out.println(2);
			session.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return data;
	}

	public ArrayList<Sv> view() {
		ArrayList<Sv> data = null;

		try {
			System.out.println(1);
			data = (ArrayList<Sv>) session.selectList("schedule.view",
					s.getCT_NO());
			System.out.println(2);
			session.close();

		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return data;
	}

	public Sv getS() {
		return s;
	}

	public void setS(Sv s) {
		this.s = s;
	}

	public ScheduleVo getSv() {
		return sv;
	}

	public void setSv(ScheduleVo sv) {
		this.sv = sv;
	}
}
